//
//  YWMatchMissionsViewController.m
//  maintain
//
//  Created by Kyo on 16/8/6.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWMatchMissionsViewController.h"
#import "UIColor+CustomColors.h"
#import "YWMacro.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "YWWorkTableViewCell.h"
#import "YWTroubleMissionCell.h"
#import "YWInspectionMisionCell.h"
#import "YWFinishKeepWatchMissionCell.h"
#import "YWMission.h"
#import "YWInspectionMissionModel.h"
#import "YWInspectionMissionManager.h"
#import "YWKeepWatchMission.h"
#import "YWNotifyManager.h"
#import "YWMissionDetailsViewController.h"
#import "YWCirculationDetailViewController.h"
#import "YWInspectionDetailViewController.h"
#import "YWInspectionLocationViewController.h"
#import "YWKeepWatchViewController.h"
#import "YWTroubleMissionModel.h"
#import "YWReportTroubleModel.h"

@interface YWMatchMissionsViewController () <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation YWMatchMissionsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择任务";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor customBackgroudColor];
    
    self.tableView.backgroundColor = [UIColor customBackgroudColor];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshTableView) name:KRefreshInspectionTimeLineNotification object:nil];
    [[YWNotifyManager shareManager] addReflashPatrolLineViewObserver:self selector:@selector(refreshTableView)];
    [[YWNotifyManager shareManager] addMissionFinishedObserver:self selector:@selector(hasDoMaintenanceMission:)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)refreshTableView{
    [self.tableView reloadData];
}
- (void)hasDoMaintenanceMission:(NSNotification*)noti
{
    if ([noti.object isKindOfClass:[YWMaintenanceMission class]]) {
        YWMaintenanceMission* updateMission = (YWMaintenanceMission*)noti.object;
        [self.matchMissions enumerateObjectsUsingBlock:^(id _Nonnull mission, NSUInteger idx, BOOL* _Nonnull stop) {
            if ([mission isKindOfClass:[YWMaintenanceMission class]]) {
                YWMaintenanceMission* theMission = mission;
                if (updateMission.missionId == theMission.missionId) {
                    theMission.completedQuantity = updateMission.completedQuantity;
                    theMission.missionStatus = updateMission.missionStatus;
                    mission = theMission;
                    *stop = YES;
                }
            }
        }];
    }else if ([noti.object isKindOfClass:[YWTroubleMissionModel class]]) {
        YWTroubleMissionModel* updateMission = (YWTroubleMissionModel*)noti.object;
        [self.matchMissions enumerateObjectsUsingBlock:^(id _Nonnull mission, NSUInteger idx, BOOL* _Nonnull stop) {
            if ([mission isKindOfClass:[YWTroubleMissionModel class]]) {
                YWTroubleMissionModel*theMission = mission;
                if (updateMission.trouble.troubleId == theMission.trouble.troubleId) {
                    theMission.troubleStatus = updateMission.troubleStatus;
                    mission = theMission;
                    *stop = YES;
                }
            }
        }];
    }
    [self.tableView reloadData];
}
-(UITableView *)tableView{
    if (!_tableView) {
        UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
        tableView.separatorColor = [UIColor customLineColor];
        tableView.sectionFooterHeight = 0.1;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.estimatedRowHeight = 44.0f;
        tableView.rowHeight = UITableViewAutomaticDimension;
        //检测保养cell
        [tableView registerClass:[YWWorkTableViewCell class] forCellReuseIdentifier:YWWorkTableViewCellIdentifier];
        //维修cell
        [tableView registerClass:[YWTroubleMissionCell class] forCellReuseIdentifier:YWTroubleMissionCellIdentifier];
        //巡查cell
        [tableView registerClass:[YWInspectionMisionCell class] forCellReuseIdentifier:YWInspectionMisionCellIdentifier];
        //巡更cell
        [tableView registerClass:[YWFinishKeepWatchMissionCell class] forCellReuseIdentifier:YWFinishKeepWatchMissionCellIdentifier];
        [self.view addSubview:tableView];
        _tableView = tableView;
    }
    return _tableView;
}

#pragma mark - UITableViewDataSource mothods
//- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
//{
//    id mission = self.matchMissions[indexPath.section];
//    if ([mission isKindOfClass:[YWMaintenanceMission class]]) {
//        return [tableView fd_heightForCellWithIdentifier:YWWorkTableViewCellIdentifier cacheByIndexPath:indexPath configuration:^(YWWorkTableViewCell *cell) {
//            [self configureWorkTableViewCell:cell atIndexPath:indexPath mission:mission];
//        }];
//    }else if ([mission isKindOfClass:[YWTroubleMissionModel class]]){
//        return [tableView fd_heightForCellWithIdentifier:YWTroubleMissionCellIdentifier cacheByIndexPath:indexPath configuration:^(YWTroubleMissionCell *cell) {
//            [self configureTroubleMissionCell:cell atIndexPath:indexPath mission:mission];
//        }];
//    }else if ([mission isKindOfClass:[YWInspectionMissionModel class]]){
//        return [tableView fd_heightForCellWithIdentifier:YWInspectionMisionCellIdentifier cacheByIndexPath:indexPath configuration:^(YWInspectionMisionCell *cell) {
//            [self configureInspectionMisionCell:cell atIndexPath:indexPath mission:mission];
//        }];
//    }else if ([mission isKindOfClass:[YWKeepWatchMission class]]){
//        return [tableView fd_heightForCellWithIdentifier:YWFinishKeepWatchMissionCellIdentifier cacheByIndexPath:indexPath configuration:^(YWFinishKeepWatchMissionCell *cell) {
//            [self configureKeepWatchMissionCell:cell atIndexPath:indexPath mission:mission];
//        }];
//    }
//    return 0;
//}

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
    return self.matchMissions.count;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    id mission = self.matchMissions[indexPath.section];
    if ([mission isKindOfClass:[YWMaintenanceMission class]]) {
        YWWorkTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:YWWorkTableViewCellIdentifier];
        [self configureWorkTableViewCell:cell atIndexPath:indexPath mission:mission];
        return cell;
    }else if ([mission isKindOfClass:[YWTroubleMissionModel class]]){
        YWTroubleMissionCell *cell = [tableView dequeueReusableCellWithIdentifier:YWTroubleMissionCellIdentifier];
        [self configureTroubleMissionCell:cell atIndexPath:indexPath mission:mission];
        return cell;
    }else if ([mission isKindOfClass:[YWInspectionMissionModel class]]){
        YWInspectionMisionCell *cell = [tableView dequeueReusableCellWithIdentifier:YWInspectionMisionCellIdentifier];
        [self configureInspectionMisionCell:cell atIndexPath:indexPath mission:mission];
        return cell;
    }else if ([mission isKindOfClass:[YWKeepWatchMission class]]){
        YWFinishKeepWatchMissionCell *cell = [tableView dequeueReusableCellWithIdentifier:YWFinishKeepWatchMissionCellIdentifier];
        [self configureKeepWatchMissionCell:cell atIndexPath:indexPath mission:mission];
        return cell;
    }
    return [[UITableViewCell alloc] init];
}

- (void)configureWorkTableViewCell:(YWWorkTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath mission:(id)mission {
    cell.fd_enforceFrameLayout = NO;
    YWMaintenanceMission *maintenanceMission = (YWMaintenanceMission*)mission;
    cell.maintenanceMission = maintenanceMission;
}
- (void)configureTroubleMissionCell:(YWTroubleMissionCell *)cell atIndexPath:(NSIndexPath *)indexPath mission:(id)mission {
    cell.fd_enforceFrameLayout = NO;
//    YWTroubleMission *troubleMission = (YWTroubleMission*)mission;
    YWTroubleMissionModel * troubleMissionModel = (YWTroubleMissionModel*)mission;
    cell.troubleMissionModel = troubleMissionModel;
}
- (void)configureInspectionMisionCell:(YWInspectionMisionCell *)cell atIndexPath:(NSIndexPath *)indexPath mission:(id)mission {
    cell.fd_enforceFrameLayout = NO;
    YWInspectionMissionModel *inspectionMission = (YWInspectionMissionModel*)mission;
    cell.missionModel = inspectionMission;
}
- (void)configureKeepWatchMissionCell:(YWFinishKeepWatchMissionCell *)cell atIndexPath:(NSIndexPath *)indexPath mission:(id)mission {
    cell.fd_enforceFrameLayout = NO;
    YWKeepWatchMission *keepWatchMission = (YWKeepWatchMission*)mission;
    cell.keepWatchMission = keepWatchMission;
}

#pragma mark - UITableViewDelegate mothods
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15;
}

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    id mission = self.matchMissions[indexPath.section];
    if ([mission isKindOfClass:[YWMaintenanceMission class]]) {
        YWMaintenanceMission *maintenanceMission = (YWMaintenanceMission*)mission;
        YWMissionDetailsViewController* missionDetailsVC = [[YWMissionDetailsViewController alloc] init];
        missionDetailsVC.mission = maintenanceMission;
        missionDetailsVC.isFromMatchMissionVC = YES;
        missionDetailsVC.qrCodeId = self.qrCodeId;
        missionDetailsVC.locationId = self.locationId;
        [self.navigationController pushViewController:missionDetailsVC animated:YES];
    }else if ([mission isKindOfClass:[YWTroubleMissionModel class]]){
//        YWTroubleMission *troubleMission = (YWTroubleMission*)mission;
        YWTroubleMissionModel* troubleMissionModel = (YWTroubleMissionModel*)mission;
        YWCirculationDetailViewController *circulationVetailVC = [[YWCirculationDetailViewController alloc] init];
        circulationVetailVC.troubleMissionModel = troubleMissionModel;
        circulationVetailVC.isFromMatchMissionVC = YES;
        [self.navigationController pushViewController:circulationVetailVC animated:YES];
    }else if ([mission isKindOfClass:[YWInspectionMissionModel class]]){
        YWInspectionMissionModel* inspectionMission = (YWInspectionMissionModel*)mission;
        if (inspectionMission.inspectionLine.lineType==YWInspectionLineTypeEquipment) {
            YWInspectionDetailViewController *detailVC = [[YWInspectionDetailViewController alloc] init];
            detailVC.inspectionMission = inspectionMission;
            detailVC.isFromMatchMissionVC = YES;
            detailVC.qrCodeId = self.qrCodeId;
            detailVC.locationId = self.locationId;
            [self.navigationController pushViewController:detailVC animated:YES];
        }else{
            YWInspectionLocationViewController *locationMissionVC = [[YWInspectionLocationViewController alloc] init];
            locationMissionVC.inspectionMission = inspectionMission;
            locationMissionVC.isFromMatchMissionVC = YES;
            locationMissionVC.qrCodeId = self.qrCodeId;
            locationMissionVC.locationId = self.locationId;
            [self.navigationController pushViewController:locationMissionVC animated:YES];
        }
    }else if ([mission isKindOfClass:[YWKeepWatchMission class]]){
        YWKeepWatchMission* keepWatchMission = (YWKeepWatchMission*)mission;
        YWKeepWatchViewController* kwvc = [[YWKeepWatchViewController alloc] init];
        kwvc.keepWatchMission = keepWatchMission;
        kwvc.isFromMatchMissionVC = YES;
        kwvc.qrCodeId = self.qrCodeId;
        kwvc.locationId = self.locationId;
        [self.navigationController pushViewController:kwvc animated:YES];
    }
}
@end
